﻿Imports DevExpress.XtraEditors

Public Class frmDonVi
    Dim Me_AddEdit As String

#Region "Proc"

    Private Sub InitControl()
        Me.txtMaDv.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.txtTenDv.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.chkHetSuDung.Properties.AppearanceFocused.BackColor = Project_FocusedColor
    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "madv"
            .Columns(i).Caption = "Mã đơn vị"
            .Columns(i).Width = 30

            i = i + 1
            .Columns(i).FieldName = "tendv"
            .Columns(i).Caption = "Tên đơn vị"
            .Columns(i).Width = 70

            i = i + 1
            .Columns(i).FieldName = "trangthai"
            .Columns(i).Visible = False

        End With

    End Sub

    Private Sub LoadGrid()

        Dim myDonViF As New DonViF
        Dim myDtb As New DataTable
        myDtb = myDonViF.GetAll

        Me.GridControl1.DataSource = myDtb

    End Sub

    Private Sub TrangThai(ByVal i As Integer)
        Select Case i
            Case 0
                Me.GridControl1.Height = Me.btnThemmoi.Top - Me.GridControl1.Top - 7
                Me.GridControl1.Enabled = True
                Me.PanelControl2.Visible = False

                Me.btnThemmoi.Text = "Thêm mới"
                Me.btnSua.Text = "Sửa"
                Me.btnXoa.Enabled = True
                Me.chkHetSuDung.Enabled = True

            Case 1
                Me.GridControl1.Height = Me.PanelControl2.Top - Me.GridControl1.Top - 7
                Me.GridControl1.Enabled = False
                Me.PanelControl2.Visible = True

                Me.btnThemmoi.Text = "Ghi"
                Me.btnSua.Text = "Bỏ qua"
                Me.btnXoa.Enabled = False

                Me.chkHetSuDung.Enabled = False

            Case 2
                Me.GridControl1.Height = Me.PanelControl2.Top - Me.GridControl1.Top - 7
                Me.GridControl1.Enabled = False
                Me.PanelControl2.Visible = True

                Me.btnThemmoi.Text = "Ghi"
                Me.btnSua.Text = "Bỏ qua"
                Me.btnXoa.Enabled = False

                Me.chkHetSuDung.Enabled = True
        End Select
    End Sub

    Private Function CheckInput() As Boolean
        If Me.txtMaDv.EditValue = "" Or Me.txtMaDv.EditValue.ToString.Length <> 2 Then
            XtraMessageBox.Show("Mã đơn vị không hợp lệ", "MOBILEsell", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.txtMaDv.Focus()
            Return False
            Exit Function
        End If

        If Me.txtTenDv.EditValue = "" Then
            XtraMessageBox.Show("Tên đơn vị không hợp lệ", "MOBILEsell", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.txtTenDv.Focus()
            Return False
            Exit Function
        End If

        Return True
    End Function

#End Region

    Private Sub frmDonVi_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmDonVi_Exists = False
    End Sub

    Private Sub frmDmMh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitControl()
        InitGrid()
        LoadGrid()
        TrangThai(0)
        
    End Sub

    Private Sub GridView1_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        If Me.GridView1.GetFocusedRowCellValue("madv") Is DBNull.Value Then Exit Sub

        Me.txtMaDv.EditValue = Me.GridView1.GetFocusedRowCellValue("madv")
        Me.txtTenDv.EditValue = Me.GridView1.GetFocusedRowCellValue("tendv")
        Me.chkHetSuDung.EditValue = Not (Me.GridView1.GetFocusedRowCellValue("trangthai"))
    End Sub

    Private Sub btnThemmoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemmoi.Click
        If Me.btnThemmoi.Text = "Thêm mới" Then
            Me_AddEdit = "add"
            TrangThai(1)

            Me.txtMaDv.EditValue = ""
            Me.txtTenDv.EditValue = ""
            Me.chkHetSuDung.EditValue = False

            Me.txtMaDv.Focus()

        ElseIf Me.btnThemmoi.Text = "Ghi" Then
            Dim myDv As New DonVi
            Dim myDvF As New DonViF
            Dim myDtb As New DataTable

            If Not CheckInput() Then Exit Sub

            myDv.MaDv = Me.txtMaDv.EditValue.ToString.Trim
            myDv.TenDv = Me.txtTenDv.EditValue.ToString.Trim
            myDv.TrangThai = Not Me.chkHetSuDung.EditValue

            If Me_AddEdit = "add" Then

                If Not myDvF.Insert(myDv) Then
                    XtraMessageBox.Show("Ghi dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    Exit Sub
                End If

                XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
                LoadGrid()
                TrangThai(0)

            ElseIf Me_AddEdit = "edit" Then
                If Not myDvF.Update(myDv) Then
                    XtraMessageBox.Show("Ghi dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    Exit Sub
                End If

                XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
                myDtb = myDvF.GetWhereMaDv(Me.txtMaDv.EditValue)
                If myDtb.Rows.Count = 1 Then
                    With Me.GridView1
                        .SetFocusedRowCellValue("madv", myDtb.Rows(0).Item("madv"))
                        .SetFocusedRowCellValue("tendv", myDtb.Rows(0).Item("tendv"))
                        .SetFocusedRowCellValue("trangthai", myDtb.Rows(0).Item("trangthai"))
                    End With
                End If
                TrangThai(0)
            End If



        End If

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If Me.btnSua.Text = "Sửa" Then
            Me_AddEdit = "edit"
            TrangThai(2)
            Me.txtMaDv.Focus()

        ElseIf Me.btnSua.Text = "Bỏ qua" Then
            TrangThai(0)
        End If

    End Sub

    Private Sub cmdXoa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim myDvF As New DonViF
        'kiem tra

        'If myMhF.ExistsMaMhInNhapCt(Me.GridView1.GetFocusedRowCellValue("mams")) Then
        '    XtraMessageBox.Show("Mặt hàng đã tồn tại trong phiếu nhập", "MOBILEsell", MessageBoxButtons.OK, MessageBoxIcon.Error)
        '    Exit Sub
        'End If

        'xoa
        If XtraMessageBox.Show("Chắc chắn xóa đơn vị?", "Quản lý bán hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            If Not myDvF.Delete(Me.GridView1.GetFocusedRowCellValue("madv")) Then
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý bán hàng", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If

            XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý bán hàng", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.GridView1.DeleteSelectedRows()
        End If

    End Sub

    Private Sub txtMaDv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaDv.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtTenDv.Focus()
    End Sub

    Private Sub txtTenDv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaDv.KeyDown
        If e.KeyCode = Keys.Enter Then Me.btnThemmoi.Focus()
    End Sub

End Class